package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.store.configure.Result;
import com.store.entity.Food;
import com.store.entity.Store;
import com.store.entity.SaleLog;
import com.store.entity.Store;
import com.store.mapper.*;
import com.store.service.StoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.utils.IDGenerator;
import com.sun.deploy.ui.DialogTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2025-01-04
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements StoreService {

    @Resource
    StoreMapper storeMapper;

    @Override
    public Result<Store> updateStore(Store store) {
        Result<Store> result = new Result<>();
        try {
            if (StringUtils.isEmpty(store.getStoreId())) {
                //如果ID为空，代表添加
                store.setStoreId(IDGenerator.getUUID("STORE"));
                store.setApprovalStatus("1");
                store.setCreateTime(new Date());
                store.setUpdateTime(new Date());
                storeMapper.insert(store);
            } else {
                //如果ID不为空，代表修改
                storeMapper.updateById(store);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Store> delStore(Store store) {
        Result<Store> result = new Result<>();
        try {
            if (StringUtils.isEmpty(store.getStoreId())) {
                result.setMessage("storeId can't be empty");
                return result;
            }
            storeMapper.deleteById(store.getStoreId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Store> batchDelStore(Store store) {
        Result<Store> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(store.getIds())) {
                String[] ids = store.getIds().split(",");
                for (String id : ids) {
                    storeMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Store> queryStore(Store store) {
        Result<Store> result = new Result<>();
        try {
            QueryWrapper<Store> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            List<Store> stores = storeMapper.selectList(wrapper);
            result.setReturnList(stores);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Store> queryStoreByPage(Store store) {
        Result<Store> result = new Result<>();

        try {
            if (store.getCurrent() == null || store.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<Store> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(store.getStoreName())) {
                wrapper.lambda().like(Store::getStoreName, store.getStoreName());
            }
            if (!StringUtils.isEmpty(store.getUserId())) {
                wrapper.lambda().like(Store::getUserId, store.getUserId());
            }

            IPage<Store> stores = storeMapper.selectPage(new Page<>(store.getCurrent(), store.getSize()), wrapper);
            result.setReturnPage(stores);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Store> queryStoreById(Store store) {
        Result<Store> result = new Result<>();
        try {
            Store returnStore = storeMapper.selectById(store.getStoreId());
            result.setReturnObj(returnStore);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Map> statistic(Store store) {
        Result<Map> result = new Result<>();

        try {
            Map<String, Object> returnMap = new HashMap<>();

            List<Map<String, String>> province = storeMapper.statisticByProvince();
            List<Map<String, String>> city = storeMapper.statisticByCity();
            List<Map<String, String>> district = storeMapper.statisticByDistrict();
            List<Map<String, String>> street = storeMapper.statisticByStreet();
            List<Map<String, String>> type = storeMapper.statisticByType();
            List<Map<String, String>> scale = storeMapper.statisticByScale();

            List<String> provinceList = new ArrayList<>();
            List<String> provinceDataList = new ArrayList<>();
            List<Map<String, Object>> provincePie = new ArrayList<>();

            for (int i = 0; i < province.size(); i++) {
                Map<String, String> item = province.get(i);
                if (i < 5) {
                    provinceList.add(item.get("province_name"));
                    provinceDataList.add(item.get("num"));
                }
                provincePie.add(new HashMap<String, Object>() {{
                    put("name", item.get("province_name"));
                    put("value", item.get("num"));
                }});
            }

            List<String> cityList = new ArrayList<>();
            List<String> cityDataList = new ArrayList<>();
            List<Map<String, Object>> cityPie = new ArrayList<>();

            for (int i = 0; i < city.size(); i++) {
                Map<String, String> item = city.get(i);

                if (i < 5) {
                    cityList.add(item.get("city_name"));
                    cityDataList.add(item.get("num"));
                }
                cityPie.add(new HashMap<String, Object>() {{
                    put("name", item.get("city_name"));
                    put("value", item.get("num"));
                }});
            }

            List<String> districtList = new ArrayList<>();
            List<String> districtDataList = new ArrayList<>();
            List<Map<String, Object>> districtPie = new ArrayList<>();

            for (int i = 0; i < district.size(); i++) {
                Map<String, String> item = district.get(i);

                if (i < 5) {
                    districtList.add(item.get("district_name"));
                    districtDataList.add(item.get("num"));
                }
                districtPie.add(new HashMap<String, Object>() {{
                    put("name", item.get("district_name"));
                    put("value", item.get("num"));
                }});
            }

            List<String> streetList = new ArrayList<>();
            List<String> streetDataList = new ArrayList<>();
            List<Map<String, Object>> streetPie = new ArrayList<>();

            for (int i = 0; i < street.size(); i++) {
                Map<String, String> item = street.get(i);

                if (i < 5) {
                    streetList.add(item.get("street_name"));
                    streetDataList.add(item.get("num"));
                }
                streetPie.add(new HashMap<String, Object>() {{
                    put("name", item.get("street_name"));
                    put("value", item.get("num"));
                }});
            }

            List<String> typeList = new ArrayList<>();
            List<String> typeDataList = new ArrayList<>();
            List<Map<String, Object>> typePie = new ArrayList<>();

            for (int i = 0; i < type.size(); i++) {
                Map<String, String> item = type.get(i);

                if (i < 5) {
                    typeList.add(item.get("store_type"));
                    typeDataList.add(item.get("num"));
                }
                typePie.add(new HashMap<String, Object>() {{
                    put("name", item.get("store_type"));
                    put("value", item.get("num"));
                }});
            }

            List<String> scaleList = new ArrayList<>();
            List<String> scaleDataList = new ArrayList<>();
            List<Map<String, Object>> scalePie = new ArrayList<>();

            for (int i = 0; i < scale.size(); i++) {
                Map<String, String> item = scale.get(i);

                if (i < 5) {
                    scaleList.add(item.get("store_scale"));
                    scaleDataList.add(item.get("num"));
                }
                scalePie.add(new HashMap<String, Object>() {{
                    put("name", item.get("store_scale"));
                    put("value", item.get("num"));
                }});
            }

            returnMap.put("provinceList", provinceList);
            returnMap.put("provinceDataList", provinceDataList);
            returnMap.put("provincePie", provincePie);

            returnMap.put("cityList", cityList);
            returnMap.put("cityDataList", cityDataList);
            returnMap.put("cityPie", cityPie);

            returnMap.put("districtList", districtList);
            returnMap.put("districtDataList", districtDataList);
            returnMap.put("districtPie", districtPie);

            returnMap.put("streetList", streetList);
            returnMap.put("streetDataList", streetDataList);
            returnMap.put("streetPie", streetPie);

            returnMap.put("typeList", typeList);
            returnMap.put("typeDataList", typeDataList);
            returnMap.put("typePie", typePie);

            returnMap.put("scaleList", scaleList);
            returnMap.put("scaleDataList", scaleDataList);
            returnMap.put("scalePie", scalePie);

            result.setMessage("success");
            result.setReturnObj(returnMap);
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }

        return result;
    }

}
